﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TerhelesEloszto.Engine;
using TerhelesEloszto.Common;

namespace TerhelesEloszto.BLTest
{
	class Program
	{
		private static IEngine e = Engine.Engine.Instance();

		private static void InsertFact()
		{
			string name;
			string neptun;
			string desc;


			Console.Write( "neptun: " );
			neptun = Console.ReadLine();

			Console.Write( "name: " );
			name = Console.ReadLine();

			Console.Write( "description: " );
			desc = Console.ReadLine();

			e.CreateFact( name, neptun, desc );

		}

		private static void UpdateFact()
		{
			string name;
			string oldNeptun;
			string desc;

			List<IFact> facts = e.GetFacts();

			Console.Write( "neptun: " );
			oldNeptun = Console.ReadLine();

			Console.Write( "name: " );
			name = Console.ReadLine();

			Console.Write( "description: " );
			desc = Console.ReadLine();


			IFact f = facts.Where( x => x.Neptun.Trim() == oldNeptun ).SingleOrDefault();
			if( f != null ) e.UpdateFact( f, name, desc );

		}

		private static void DeleteFact()
		{
			string neptun;
			List<IFact> facts = e.GetFacts();

			Console.Write( "neptun: " );
			neptun = Console.ReadLine();

			IFact f = facts.Where( x => x.Neptun.Trim() == neptun ).SingleOrDefault();
			if( f != null ) e.RemoveFact( f );
		}

		private static void ListFacts()
		{
			foreach( IFact fact in e.GetFacts() )
			{
				Console.WriteLine( "{0}, {1}, {2}", fact.Neptun.Trim(), fact.Name.Trim(), fact.Description.Trim() );
			}
		}

		private static void Test()
		{
			int nbr = Convert.ToInt32( Console.ReadLine() );
			e.Test( nbr );
		}

		static void Main( string[] args )
		{

			char key = ' ';

			while( key != 'q' )
			{
				Console.WriteLine( "========================" );
				Console.WriteLine( "[I]nser fact" );
				Console.WriteLine( "[U]pdate fact" );
				Console.WriteLine( "[L]ist facts" );
				Console.WriteLine( "[D]elete fact" );
				Console.WriteLine( "[T]est" );
				Console.WriteLine( "[Q]uit" );
				Console.WriteLine( "========================" );
				key = Console.ReadLine().First();

				try
				{
					switch( key )
					{
						case 'i':
							InsertFact();
							break;
						case 'l':
							ListFacts();
							break;
						case 'u':
							UpdateFact();
							break;
						case 'd':
							DeleteFact();
							break;
						case 't':
							Test();
							break;
						default:
							break;
					}
				}
				catch( EngineException ex )
				{
					Console.WriteLine( "Error: " + ex.Message );
				}
			}


		}
	}
}
